<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息列表页</title>
<!-- 下载并引入jquery easyUI -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/gray/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath}/jquery-easyui-1.8.8/themes/color.css">
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath}/jquery-easyui-1.8.8/jquery.easyui.min.js"></script>


</head>
<body>
	<table id="student_dg" title="学生信息列表" class="easyui-datagrid"
		style="width: 1150px; height: 480px"
		url="${param.request.contextPath}/admin/studentindex"
		toolbar="#student_toolbar" pagination="true" rownumbers="true"
		fitColumns="true" singleSelect="true">
		<thead>
			<tr>
				<th field="cb" checkbox="true" align="center"></th>
				<th field="stuId" width="50">学号</th>
				<th field="stuName" width="50">姓名</th>
				<th field="sex" formatter="sexFormatter" width="50">性别</th>
				<th field="birthdate" width="50">出生日期</th>
				<th field="place" width="50">籍贯</th>
				<th field="address" width="50">家庭住址</th>
				
				<th field="custName" formatter="custodianFormatter" width="50">监护人</th>
				<th field="claId" formatter="classFormatter" width="50">班级编号</th>
				<th field="dormId" formatter="dormFormatter" width="50">宿舍编号</th>
				<th field="cardId" formatter="spendsFormatter" width="50">校园卡号</th>
			</tr>
		</thead>
	</table>

	<!-- 工具条组件 -->
	<div id="student_toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="newStudent()">新增</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" plain="true" onclick="editStudent()">修改</a> <a
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" plain="true" onclick="destroyStudent()">删除</a>
	</div>

	<!-- 新增和修改表单  start -->
	<div id="student_dlg" class="easyui-dialog"
		style="width: 400px; height: 280px; padding: 10px 20px" closed="true"
		buttons="#student_dlg-buttons">
		<div class="ftitle">学生信息管理</div>
		<form id="student_fm" enctype="multipart/form-data" method="post">
			<div class="fitem">
				<label>姓名:</label> <input name="stuName"
					class="easyui-validatebox" required="true">
			</div>
			<div class="fitem">
				<label>性别:</label> <input id="sex" name="sex" value=10
					style="width: 80px;" >
			</div>
			<div class="fitem">
				<label>出生日期:</label> <input name="birthdate" value=10
					class="easyui-datebox" style="width: 80px;" >
			</div>
			<div class="fitem">
				<label>籍贯:</label> <input name="place" class="easyui-validatebox">
			</div>
			<div class="fitem">
				<label>家庭地址:</label> <input name="address"
					class="easyui-validatebox">
			</div>
			
			
			<div class="fitem">
				<label>监护人:</label> <input id="custName"
					name="custodian.custId" class="easyui-combobox"
					data-options="valueField:'custId',textField:'custName',
					url:'${param.request.contextPath}/custodian/all'">
			</div>
			<div class="fitem">
				<label>班级编号:</label> <input id="claId"
					name="class.claId" class="easyui-combobox"
					data-options="valueField:'claId',textField:'claId',
					url:'${param.request.contextPath}/class/all'">
			</div>
			<div class="fitem">
				<label>宿舍编号:</label> <input id="dormId"
					name="dorm.dormId" class="easyui-combobox"
					data-options="valueField:'dormId',textField:'dormId',
					url:'${param.request.contextPath}/dorm/all'">
			</div>
			<div class="fitem">
				<label>校园卡号:</label> <input id="cardId"
					name="spends.cardId" class="easyui-combobox"
					data-options="valueField:'cardId',textField:'cardId',
					url:'${param.request.contextPath}/spends/all'">
			</div>
			
		</form>
	</div>
	<div id="student_dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok"
			onclick="saveStudent()">保存</a> <a href="#" class="easyui-linkbutton"
			iconCls="icon-cancel"
			onclick="javascript:$('#student_dlg').dialog('close')">取消</a>
	</div>

	<!-- 新增和修改表单 end -->

	<script type="text/javascript">

	$('#sex').combobox({
		 data:[
			 {
				  "sexId": 0,
				  "sexName": "女"
				}
				,{
				  "sexId": 1,
				  "sexName": "男"
				}
				],
      valueField:'sexId',
      textField:'sexName',
      required:'true'
      });

		function sexFormatter(value, row, index) {
			if (value == 1) {
				return '男';
			}
			return '女';
		}

		function custodianFormatter(value,row,index){
			if(row.custodian == null){
				return null;

				}
			return row.custodian.custName;
			}


		function classFormatter(value,row,index){
			if(row.classs == null){
				return null;

				}
			return row.classs.claId;
			}

		function dormFormatter(value,row,index){
			if(row.dorm == null){
				return null;

				}
			return row.dorm.dormId;
			}

		function spendsFormatter(value,row,index){
			if(row.spends == null){
				return null;

				}
			return row.spends.cardId;
			}

		//下拉列表反选的方法
		function loadSuccess1(custId) {
			var data = $("#custName").combobox("getData");
			if (data && data.length > 0) {
				if (custId == 0) {
					$("#custName").combobox("setValue", data[0]['custId']);
				} else {
					$("#custName").combobox("setValue", custId);
				}
			}
		}

		function loadSuccess2(claId) {
			var data = $("#claId").combobox("getData");
			if (data && data.length > 0) {
				if (claId == 0) {
					$("#claId").combobox("setValue", data[0]['claId']);
				} else {
					$("#claId").combobox("setValue", claId);
				}
			}
		}

		function loadSuccess3(dormId) {
			var data = $("#dormId").combobox("getData");
			if (data && data.length > 0) {
				if (dormId == 0) {
					$("#dormId").combobox("setValue", data[0]['dormId']);
				} else {
					$("#dormId").combobox("setValue", dormId);
				}
			}
		}

		function loadSuccess4(cardId) {
			var data = $("#cardId").combobox("getData");
			if (data && data.length > 0) {
				if (cardId == 0) {
					$("#cardId").combobox("setValue", data[0]['cardId']);
				} else {
					$("#cardId").combobox("setValue", cardId);
				}
			}
		}

		// 新增
		function newStudent() {
			$('#student_dlg').dialog('open').dialog('setTitle', '新增学生信息');
			$('#student_fm').form('clear');
			url = '${pageContext.request.contextPath}/student/add';
			loadSuccess1(0);
			loadSuccess2(0);
			loadSuccess3(0);
			loadSuccess4(0);
			
		}

		// 修改
		function editStudent() {
			//$('#student_fm').form('clear');
			var row = $('#student_dg').datagrid('getSelected');
			if (row) {
				$('#student_dlg').dialog('open').dialog('setTitle', '修改学生信息');
				$('#student_fm').form('load', row);
				url = '${pageContext.request.contextPath}/student/edit?stuId='
						+ row.stuId;
				loadSuccess(row.custodian.custId);
				loadSuccess(row.classs.claId);
				loadSuccess(row.dorm.dromId);
				loadSuccess(row.spends.cardId);
			}
		}

		function saveStudent() {
			$('#student_fm').form('submit', {
				url : url,
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(result) {
					var result = eval('(' + result + ')');
					if (!result.success) {
						$.messager.show({
							title : 'Error',
							msg : result.errorMsg
						});
					} else {
						$.messager.show({
							title : 'Info',
							msg : result.errorMsg
						});
					}

					$('#student_dlg').dialog('close'); // close the dialog
					$('#student_dg').datagrid('reload'); // reload the user data
				}
			});
		}

		function destroyStudent() {
			var row = $('#student_dg').datagrid('getSelected');
			if (row) {
				$.messager
						.confirm(
								'Confirm',
								'确认删除学生信息?',
								function(r) {
									if (r) {
										$
												.post(
														'${pageContext.request.contextPath}/student/delete',
														{
															stuId : row.stuId
														},
														function(result) {
															if (result.success) {
																$('#student_dg')
																		.datagrid(
																				'reload'); // reload the user data
															} else {
																$.messager
																		.show({ // show error message
																			title : 'Error',
																			msg : result.errorMsg
																		});
															}
														}, 'json');
									}
								});
			}
		}
	</script>
</body>
</html>